package com.sqx.modules.tbOrders.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.dao.UserDao;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.goods.dao.GoodsDao;
import com.sqx.modules.goods.entity.Goods;
import com.sqx.modules.sys.service.SysUserRoleService;
import com.sqx.modules.sys.service.SysUserService;
import com.sqx.modules.tbOrders.dao.ComplaintDao;
import com.sqx.modules.tbOrders.dao.IllegalTypeDao;
import com.sqx.modules.tbOrders.dao.TbOrdersDao;
import com.sqx.modules.tbOrders.entity.IllegalType;
import com.sqx.modules.tbOrders.entity.SysComplaint;
import com.sqx.modules.tbOrders.entity.TbOrders;
import com.sqx.modules.tbOrders.service.ComplaintService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintDao, SysComplaint> implements ComplaintService {

    @Autowired
    private IllegalTypeDao illegalTypeDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private TbOrdersDao tbOrdersDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private CommonInfoService commonInfoService;

    @Override
    public Result findAllComplaint(Long userId, Integer illegalId, Integer complaintState, Integer page, Integer limit,String userName) {
        Page<SysComplaint> pages = new Page<>(page, limit);
        PageUtils pageUtils = new PageUtils(baseMapper.findAppComplaint(pages, userId, null, complaintState,userName));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result insertComplaint(SysComplaint sysComplaint) {
        if (sysComplaint.getOrderType() == 0) {
            //添加投诉记录
            TbOrders tbOrders = tbOrdersDao.selectOne(new QueryWrapper<TbOrders>().eq("order_id", sysComplaint.getOrderNumber()));
            if (tbOrders != null) {
                Goods goods = goodsDao.selectById(tbOrders.getGoodId());
                if (goods != null) {
                    sysComplaint.setUserId(sysComplaint.getUserId());
                    sysComplaint.setOrderNumber(tbOrders.getOrderNumber());
                    sysComplaint.setComplaintTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                    sysComplaint.setComplaintState(5);
                    sysComplaint.setOrderType(tbOrders.getRetrieveType());
                    sysComplaint.setDefendantUserId(tbOrders.getBuyerId());
                    baseMapper.insert(sysComplaint);
                    return Result.success();
                }
            } else {
                return Result.error("订单不存在！");
            }
        } else if (sysComplaint.getOrderType() == 1) {
            //添加投诉记录
            TbOrders tbOrders = tbOrdersDao.selectOne(new QueryWrapper<TbOrders>().eq("order_id", sysComplaint.getOrderNumber()));
            if (tbOrders != null) {
                Goods goods = goodsDao.selectById(tbOrders.getGoodId());
                if (goods != null) {
                    sysComplaint.setUserId(sysComplaint.getUserId());
                    sysComplaint.setOrderNumber(tbOrders.getOrderNumber());
                    sysComplaint.setComplaintTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                    sysComplaint.setComplaintState(5);
                    sysComplaint.setOrderType(tbOrders.getRetrieveType());
                    sysComplaint.setDefendantUserId(tbOrders.getBuyerId());
                    baseMapper.insert(sysComplaint);
                    return Result.success();
                }
            } else {
                return Result.error("订单不存在！");
            }
        } else if (sysComplaint.getOrderType() == 2) {
            Goods goods = goodsDao.selectById(sysComplaint.getGoodId());
            if (goods != null) {
                sysComplaint.setUserId(sysComplaint.getUserId());
                sysComplaint.setOrderNumber(String.valueOf(goods.getGoodId()));
                sysComplaint.setComplaintTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                sysComplaint.setComplaintState(5);
                sysComplaint.setOrderType(sysComplaint.getOrderType());
                sysComplaint.setDefendantUserId(goods.getUserId());
                baseMapper.insert(sysComplaint);
                return Result.success();
            } else {
                return Result.error("商品不存在！");
            }
        } else {
            UserEntity user = userDao.selectById(sysComplaint.getUserId());
            if (user == null){
                return Result.error("用户不存在！");
            }
            sysComplaint.setUserId(sysComplaint.getUserId());
            sysComplaint.setComplaintTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            sysComplaint.setComplaintState(5);
            sysComplaint.setOrderType(sysComplaint.getOrderType());
            sysComplaint.setDefendantUserId(sysComplaint.getDefendantUserId());
            baseMapper.insert(sysComplaint);
            return Result.success();
        }
        return Result.error("参数错误，请检查");
    }

    @Override
    public Result findComplaint(Long userId, Long complaintId) {
        SysComplaint sysComplaint = baseMapper.findComplaint(complaintId);
        return Result.success().put("data", sysComplaint);
    }

    @Override
    public Result appealIndent(Long userId, SysComplaint sysComplaint) {
        sysComplaint.setComplaintState(2);
        sysComplaint.setAppealTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        baseMapper.updateById(sysComplaint);
        return Result.success();
    }

    @Override
    public IPage<SysComplaint> findAllAppeal(Integer page, Integer limit, SysComplaint complaint) {
        Page<SysComplaint> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.findAllComplaint(pages, complaint);

    }

    @Override
    public IPage<SysComplaint> getUserComplaint(Long userId, Integer page, Integer limit,Integer complaintState,String userName) {
        Page<SysComplaint> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.getUserComplaint(pages, userId,complaintState,userName);

    }

    @Override
    public Result updateCustomerComplaint(Long complaintId, Integer complaintState) {
        if (complaintState == 1) {
            //投诉通过，状态改为1
            SysComplaint sysComplaint = new SysComplaint();
            sysComplaint.setComplaintId(complaintId);
            sysComplaint.setComplaintState(1);
            baseMapper.updateById(sysComplaint);
        } else {
            //投诉未通过，修改投诉状态为6
            SysComplaint sysComplaint = new SysComplaint();
            sysComplaint.setComplaintId(complaintId);
            sysComplaint.setComplaintState(6);
            baseMapper.updateById(sysComplaint);
        }
        return Result.success();
    }

    @Override
    public Result updateComplaintState(SysComplaint sysComplaint) {
        baseMapper.updateById(sysComplaint);
        return Result.success();
    }

}
